/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author yulian
 */
public class SocketGet extends Thread{
    private ServerSocket fServerSocket;
    private BufferedReader reader;
    
    BufferedInputStream entrada;
    JTextArea messages;
    JTextField message;
    
    public SocketGet(int puerto,JTextField message,JTextArea messages){
    
        try{
            
        fServerSocket= new ServerSocket();
        this.messages=messages;
        this.message=message;
        
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void run(){
        Socket client=null;
        while(true){
            if(fServerSocket == null) return;
            try{
                client=new Socket();
                client=fServerSocket.accept();
                InetAddress address=client.getInetAddress();//saca la direccion del cliente
                entrada=new BufferedInputStream(client.getInputStream());
                
                int ien=0;
                byte []buffer=new byte[1024];
                ien = entrada.read(buffer);
                if(ien > 0){
                    String line = new String(buffer);
                    messages.append(address.toString()+": "+line.trim()+"\n");
                }
                messages.setSelectionEnd(message.getText().length());
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
